package com.qumitech.visualnovel.controller.rest;

import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.service.userexpand.UserExpandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/user/expand")
public class UserExpandController {
    @Autowired
    private UserExpandService userExpandService;

    @GetMapping("/relation")
    public BaseRsp listRelation(@RequestParam(value = "fellow_uid", required = false) Long fellowUid,
                                @RequestParam(value = "master_uid", required = false) Long masterUid,
                                @RequestParam(value = "from_bind_time", required = false) String fromBindTime,
                                @RequestParam(value = "to_bind_time", required = false) String toBindTime,
                                @RequestParam(value = "offset", required = true) Integer offset,
                                @RequestParam(value = "limit", required = true) Integer limit){
        Map<String, String> reqMap = new HashMap<>();
        if (fellowUid != null){
            reqMap.put("fellow_uid", String.valueOf(fellowUid));
        }
        if (masterUid != null){
            reqMap.put("master_uid", String.valueOf(masterUid));
        }
        if (fromBindTime != null){
            reqMap.put("from_bind_time", fromBindTime);
        }
        if (toBindTime != null){
            reqMap.put("to_bind_time", toBindTime);
        }
        if (offset != null){
            reqMap.put("offset", offset.toString());
        }
        if (limit != null){
            reqMap.put("limit", limit.toString());
        }
        return userExpandService.listRelation(reqMap);
    }

    @GetMapping("/profit/list")
    public BaseRsp listProfit(@RequestParam(value = "masterUid", required = false) Long accountId,
                              @RequestParam(value = "minProfit", required = false) Long minProfit,
                              @RequestParam(value = "maxProfit", required = false) Long maxProfit,
                              @RequestParam(value = "startTime", required = false) String startTime,
                              @RequestParam(value = "endTime", required = false) String endTime,
                              @RequestParam(value = "orderBy", required = false, defaultValue = "income") String orderBy,
                              @RequestParam(value = "orderType", required = false, defaultValue = "desc") String orderType,
                              @RequestParam(value = "offset", required = false, defaultValue = "0") Integer offset,
                              @RequestParam(value = "limit", required = false, defaultValue = "20") Integer limit) {
        return userExpandService.listProfit(accountId, minProfit, maxProfit, startTime, endTime, orderBy, orderType,offset, limit);
    }

    @GetMapping("/profit/detail")
    public BaseRsp listProfitDetail(@RequestParam(value = "masterUid", required = false) Long masterUid,
                                    @RequestParam(value = "fellowUid", required = false) Long fellowUid,
                                    @RequestParam(value = "startTime", required = false) String startTime,
                                    @RequestParam(value = "endTime", required = false) String endTime,
                                    @RequestParam(value = "offset", required = false, defaultValue = "0") Integer offset,
                                    @RequestParam(value = "limit", required = false, defaultValue = "20") Integer limit) {
        return userExpandService.listProfitDetail(masterUid, fellowUid, startTime, endTime, offset, limit);
    }

}
